﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using dhDataAcess.dataAcess.sysDataSetTableAdapters;
using dhDataAcess.dataAcess;
using dhDataAcess.Model;
using dhDataAcess.sysDAL;
using System.Data;
using System.Data.SqlClient;
using dhDataAcess.baseBLL;

namespace dhSystemBLL.basicData
{
    public class deptBLL
    {
        private deptListTableAdapter _deptAdapter = null;
        private deptListTableAdapter deptAdapter
        {
            get
            {
                if (_deptAdapter == null) _deptAdapter = new deptListTableAdapter();
                return _deptAdapter;
            }
        }
        /// <summary>
        /// 查询部门表
        /// </summary>
        /// <returns></returns>

        public sysDataSet.deptListDataTable getdeptListAll()
        {
            try
            {
                return deptAdapter.GetDatadeptAll ();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }

        /// <summary>
        /// 根据部门名称查询部门表
        /// </summary>
        /// <param name="deptNum"></param>
        /// <returns></returns>

        public sysDataSet.deptListDataTable getdeptListAll(string deptName)
        {
            try
            {

                return deptAdapter.GetDataByDeptListByName (deptName);
                
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }


     /*   //插入
        public int deptListInsert(sysDataSet.deptListRow row)
        {
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
           
            try
            {
                tn = conn.BeginTransaction();
                deptListDAL dept = new deptListDAL(tn, conn);
                dept.deptListInsert(row);
                tn.Commit();
                return i;
            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());
            }
            finally
            {
                conn.Close();
            }
        }
        */
        /// <summary>
        /// 插入
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>

        public int deptUpdate(sysDataSet.deptListDataTable pp)
        {
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                deptListDAL dept = new deptListDAL(tn, conn);

                foreach (sysDataSet.deptListRow row in pp)
                {
                    if (row.RowState == DataRowState.Modified)
                    {
                        //调用更改的过程 
                    }
                    if (row.RowState == DataRowState.Added)
                    {
                    }
                }

                tn.Commit();
                return i;
            }
            catch (SqlException ex)
            {
                tn.Rollback();
                if (ex.Number == 2627)
                {
                    throw new Exception("这个部门编号已经存在");
                }
                if (ex.Number == 2601)
                {
                    throw new Exception("部门名称不能重复");
                }

                throw new Exception(ex.Number.ToString() + ex.Message.ToString());

            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());
            }

            finally
            {
                conn.Close();
            }
        }

        public int deptListInsert(deptListModel row)
        {
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                deptListDAL dept = new deptListDAL(tn, conn);
                dept.deptListInsert(row);
                tn.Commit();
                return i;
            }
            catch (SqlException ex)
            {
                tn.Rollback();
                if (ex.Number == 2627)
                {
                    throw new Exception("这个部门编号已经存在");
                }
                if (ex.Number == 2601)
                {
                    throw new Exception("部门名称不能重复");
                }

                throw new Exception(ex.Number.ToString() + ex.Message.ToString());

            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());
            }

            finally
            {
                conn.Close();
            }
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>

        public int deptListUpdate(deptListModel row)
        {
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
              
                deptListDAL dept = new deptListDAL(tn, conn);
                dept.deptListUpdate(row);
                tn.Commit();
                return i;
            }
            catch (Exception ex)
            {
                tn .Rollback ();
                throw new Exception (ex.Message .ToString ());
            }
            finally 
            {
                conn .Close ();
            }
            }
            /// <summary>
        /// 删除
            /// </summary>
            /// <param name="deptNum"></param>
            /// <returns></returns>

        public int deptListDel(string deptNum)
        {
            int i=0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                deptListDAL dept = new deptListDAL(tn, conn);
                dept.deptListDel(deptNum );
                tn.Commit();
                return i;
            }
            catch(Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());
            }
            finally
            {
                conn.Close();
            }
        }
        /// <summary>
        /// 根据部门编号返回一个部门名称
        /// </summary>
        /// <param name="deptNum"></param>
        /// <returns></returns>

        public int deptListRetName(string deptNum)
        {
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                deptListDAL dept = new deptListDAL(tn, conn);
                dept.deptListRetName(deptNum);
                tn.Commit();
                return i;
            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());
            }
            finally
            {
                conn.Close();
            }
        }
        }

    }

